<template>
	<view class="classify">
		<view class="top-search">
			<view class="l">
				<picker @change="navChange" :value="arrayIndex" :range="array" style="width: 100%;" @cancel="cancel">
					<view class="picker">
						<text v-if="navName">{{navName}}</text>
						<text v-else>分类搜索</text>
						<view class="arrows">
						</view>
					</view>
				</picker>
			</view>
			<view class="r">
				<div class="input">
					<input type="text" v-model="kw" placeholder="请输入关键词">
				</div>
				<view class="search" @click="search">
					搜索
				</view>
			</view>
		</view>
		<view class="m_content">
			<view class="m_list" v-for="v in list" :key="v.id" :data-id="v.id" @click="toDetails">
				<view class="m_img">
					<image v-if="v.image" :src="http+v.image" mode=""></image>
				</view>
				<view class="m_text">
					<view class="m_text_t">
						{{v.title}}
					</view>
					<view class="m_label">
						<view class="label" v-for="x in v.labelList" :key="x.id">
							{{x.name}}
						</view>
					</view>
					<view class="" style="height: 50rpx;">

					</view>
					<view class="m_text_c">
						￥{{v.price}}
					</view>
				</view>
				<view class="hint">
					{{arr[index]}}
				</view>
			</view>
			<view class="kong" v-if="list.length==0">
				<image src="../../static/mine/kong.png" mode=""></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				arr: ['热门', '陶公塾', '旅行', '瓷星人', '活动'],
				index: null,
				list: [],
				http: this.$http,
				allpage: 1,
				page: 1,
				id: '',
				array: ['中国中国中国中国中国中国', '美国', '巴西', '日本'],
				arrayIndex: 0,
				navName: "",
				navList: [],
				name: "",
				kw: '',
				isclick:false
			}
		},
		onLoad(o) {
			// console.log(o,111)
			this.index = o.type
			if (o.id) {
				this.id = o.id
			}
			if (o.name) {
				this.name = o.name
			}
			this.getNavList()
			this.getList()

		},
		onReachBottom() {
			if (this.page < this.allpage) {
				this.page++
				if (this.index == 1) {
					this.$post('tickets/ticketList', {
						page: this.page,
						cate_id: this.id,
						title:this.kw
					}).then(res => {
						// console.log(res)
						if (res.code == 0) {
							this.list = this.list.concat(res.data.list)
							this.allpage = res.data.allpage
						}
					})
				}
				if (this.index == 2) {
					this.$post('lines/lineList', {
						page: this.page,
						cate_id: this.id,
						title:this.kw
					}).then(res => {
						// console.log(res)
						if (res.code == 0) {
							this.list = this.list.concat(res.data.list)
							this.allpage = res.data.allpage
						}
					})
				}
				if (this.index == 3) {
					this.$post('star/starList', {
						page: this.page,
						cate_id: this.id,
						title:this.kw
					}).then(res => {
						// console.log(res)
						if (res.code == 0) {
							this.list = this.list.concat(res.data.list)
							this.allpage = res.data.allpage
						}
					})
				}
				if (this.index == 4) {
					this.$post('hotels/hotelList', {
						page: this.page,
						cate_id: this.id,
						title:this.kw
					}).then(res => {
						// console.log(res)
						if (res.code == 0) {
							this.list = this.list.concat(res.data.list)
							this.allpage = res.data.allpage
						}
					})
				}
			} else {
				uni.showToast({
					title: '暂无更多数据',
					icon: 'none'
				})
			}
		},
		onShareAppMessage(e) {
			return {
				title: '',
				path: '/pages/index/index'
			}
		},
		methods: {
			search() {
				this.list = []
				this.page = 1
				this.getList()
			},
			getNavList() {
				this.$post('cates/cateList', {

				}).then(res => {
					console.log(res)
					if (res.code == 0) {
						if (this.index == 1) {
							this.navList = res.data.ticketcateList
						}
						if (this.index == 2) {
							this.navList = res.data.linecateList
						}
						if (this.index == 3) {
							this.navList = res.data.storecateList
						}
						if (this.index == 4) {
							this.navList = res.data.hotelcateList
						}
						this.array = this.navList.map(item => item.name)
					}

				})
			},

			getList() {
				if (this.index == 1) {
					if (this.name) {
						uni.setNavigationBarTitle({
							title: this.name
						})
					} else {
						uni.setNavigationBarTitle({
							title: "陶公塾"
						})
					}

					this.$post('tickets/ticketList', {
						page: 1,
						cate_id: this.id,
						title:this.kw
					}).then(res => {
						// console.log(res)
						if (res.code == 0) {
							this.list = res.data.list
							this.allpage = res.data.allpage
						}
						if (res.code == -3) {
							uni.showToast({
								title: '用户不存在请重新登录',
								icon: 'none',
								mask: true
							})
							uni.clearStorage()
							setTimeout(() => {
								uni.switchTab({
									url: 'index'
								})
							}, 1200)
						}
						if (res.code == -4) {
							uni.showToast({
								title: '用户已被禁用',
								icon: 'none',
								mask: true
							})
							// uni.clearStorage()
							setTimeout(() => {
								uni.switchTab({
									url: 'index'
								})
							}, 1200)
						}
					})
				}
				if (this.index == 2) {
					if (this.name) {
						uni.setNavigationBarTitle({
							title: this.name
						})
					} else {
						uni.setNavigationBarTitle({
							title: "旅游"
						})
					}
					this.$post('lines/lineList', {
						page: 1,
						cate_id: this.id,
						title:this.kw
					}).then(res => {
						// console.log(res)
						if (res.code == 0) {
							this.list = res.data.list
							this.allpage = res.data.allpage
						}
						if (res.code == -3) {
							uni.showToast({
								title: '用户不存在请重新登录',
								icon: 'none',
								mask: true
							})
							uni.clearStorage()
							setTimeout(() => {
								uni.switchTab({
									url: 'index'
								})
							}, 1200)
						}
						if (res.code == -4) {
							uni.showToast({
								title: '用户已被禁用',
								icon: 'none',
								mask: true
							})
							// uni.clearStorage()
							setTimeout(() => {
								uni.switchTab({
									url: 'index'
								})
							}, 1200)
						}
					})
				}
				if (this.index == 3) {
					if (this.name) {
						uni.setNavigationBarTitle({
							title: this.name
						})
					} else {
						uni.setNavigationBarTitle({
							title: "瓷星人"
						})
					}
					this.$post('star/starList', {
						page: 1,
						cate_id: this.id,
						title:this.kw
					}).then(res => {
						// console.log(res)
						if (res.code == 0) {
							this.list = res.data.list
							this.allpage = res.data.allpage
						}
						if (res.code == -3) {
							uni.showToast({
								title: '用户不存在请重新登录',
								icon: 'none',
								mask: true
							})
							uni.clearStorage()
							setTimeout(() => {
								uni.switchTab({
									url: 'index'
								})
							}, 1200)
						}
						if (res.code == -4) {
							uni.showToast({
								title: '用户已被禁用',
								icon: 'none',
								mask: true
							})
							// uni.clearStorage()
							setTimeout(() => {
								uni.switchTab({
									url: 'index'
								})
							}, 1200)
						}
					})
				}
				if (this.index == 4) {
					if (this.name) {
						uni.setNavigationBarTitle({
							title: this.name
						})
					} else {
						uni.setNavigationBarTitle({
							title: "活动"
						})
					}
					this.$post('hotels/hotelList', {
						page: 1,
						cate_id: this.id,
						title:this.kw
					}).then(res => {
						// console.log(res)
						if (res.code == 0) {
							this.list = res.data.list
							this.allpage = res.data.allpage
						}
						if (res.code == -3) {
							uni.showToast({
								title: '用户不存在请重新登录',
								icon: 'none',
								mask: true
							})
							uni.clearStorage()
							setTimeout(() => {
								uni.switchTab({
									url: 'index'
								})
							}, 1200)
						}
						if (res.code == -4) {
							uni.showToast({
								title: '用户已被禁用',
								icon: 'none',
								mask: true
							})
							// uni.clearStorage()
							setTimeout(() => {
								uni.switchTab({
									url: 'index'
								})
							}, 1200)
						}
					})
				}
			},

			cancel(e) {
				if(this.isclick){
					this.arrayIndex = 0
					this.navName = ''
					this.id = ''
					this.list = []
					this.page = 1
					this.getList()
				}
			},
			navChange(e) {
				this.isclick = true
				this.arrayIndex = e.detail.value
				this.navName = this.array[this.arrayIndex]
				this.id = this.navList[this.arrayIndex].id
				this.list = []
				this.page = 1
				this.getList()
			},
			toDetails(e) {
				let id = e.currentTarget.dataset.id
				if (this.index == 1) {
					uni.navigateTo({
						url: 'details?id=' + id
					})
				}
				if (this.index == 2) {
					uni.navigateTo({
						url: 'hotel?id=' + id
					})
				}
				if (this.index == 3) {
					uni.navigateTo({
						url: 'restaurant?id=' + id
					})
				}
				if (this.index == 4) {
					uni.navigateTo({
						url: 'hotel2?id=' + id
					})
				}
			}
		}
	}
</script>

<style lang="less">
	.top-search {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		height: 80rpx;
		line-height: 80rpx;
		padding: 0 30rpx;
		background-color: #fff;
		z-index: 9999;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.l {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 150rpx;

			.picker {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			text {
				font-size: 26rpx;
				color: #666;
				flex: 1;
				width: 1rpx;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}

			.arrows {
				margin-top: -10rpx;
				margin-left: 10rpx;
				width: 14rpx;
				height: 14rpx;
				border-top: 4rpx solid #666666;
				border-right: 4rpx solid #666666;
				transform: rotate(135deg);
			}
		}

		.r {
			margin-left: 30rpx;
			flex: 1;
			width: 1rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.input {
				flex: 1;
				width: 1rpx;
				border: 2rpx solid #eee;
				border-radius: 6rpx;
				height: 60rpx;
				line-height: 60rpx;
				padding: 0 20rpx;
				border-top-left-radius: 30rpx;
				border-bottom-left-radius: 30rpx;
				box-sizing: border-box;

				input {
					height: 60rpx;
					line-height: 60rpx;
					font-size: 26rpx;
					color: #666;
				}

			}

			.search {
				height: 60rpx;
				background-color: #FF7416;
				color: #fff;
				font-size: 26rpx;
				text-align: center;
				line-height: 60rpx;
				width: 150rpx;
				border-radius: 30rpx;
				border-top-left-radius: 0;
				border-bottom-left-radius: 0;
			}
		}
	}

	.classify {
		padding-top: 100rpx;
		.m_content {
			padding-left: 30rpx;
			padding-right: 30rpx;
			box-sizing: border-box;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.kong {
				width: 450rpx;
				height: 287rpx;
				margin: 0 auto;
				margin-top: 10%;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.m_list {
				width: 340rpx;
				// height: 423rpx;
				opacity: 1;
				background: #ffffff;
				border-radius: 20rpx;
				box-shadow: -4rpx 0rpx 14rpx 0px rgba(227, 227, 227, 0.35);
				margin-bottom: 10rpx;
				position: relative;

				.hint {
					position: absolute;
					top: 34rpx;
					left: 0;
					line-height: 36rpx;
					text-align: center;
					opacity: 1;
					background: #FF7416;
					border-radius: 0rpx 18rpx 18rpx 0rpx;
					font-size: 24rpx;
					font-weight: 700;
					color: #fff;
					padding: 0 10rpx;
				}

				.m_img {
					width: 340rpx;
					height: 267rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.m_text {
					font-size: 28rpx;
					font-weight: 700;
					color: #333333;
					padding: 14rpx 34rpx;
					box-sizing: border-box;

					.m_label {
						display: flex;
						flex-wrap: wrap;
						margin-top: 10rpx;

						.label {
							padding: 2rpx 6rpx;
							opacity: 1;
							border: 1rpx solid #b4b4b4;
							border-radius: 5rpx;
							color: #666666;
							font-weight: 400;
							font-size: 18rpx;
							margin-right: 4rpx;
							margin-bottom: 4rpx;
						}
					}

					.m_text_c {
						font-weight: 700;
						color: #FF7416;
						font-size: 30rpx;
						margin-top: 12rpx;
						position: absolute;
						bottom: 20rpx;
						left: 34rpx;
					}
				}
			}
		}
	}
</style>